using System;
using System.Xml.Serialization;
using RaisingStudio.Common.Reflection;

namespace RaisingStudio.Data.Common.Configuration
{
	/// <summary>
	/// Alias.
	/// </summary>
	[Serializable]
	[XmlRoot("Alias")]
	public class Alias
	{
		#region Fields
		[NonSerialized]
		private string _name = string.Empty;
		[NonSerialized]
		private string _typeName = string.Empty;
		[NonSerialized]
		private Type _type = null;
		#endregion

		#region Properties
		/// <summary>
		/// Name used to identify the typeAlias amongst the others.
		/// </summary>
		[XmlAttribute("name")]
		public string Name
		{
			get { return _name; }
			set 
			{ 
				if ((value == null) || (value.Length < 1))
				{
					throw new ArgumentNullException("The name attribute is mandatory in the typeAlias ");
				}
				_name = value; 
			}
		}


		/// <summary>
		/// The type class for the typeAlias
		/// </summary>
		[XmlIgnore]
		public Type Type
		{
			get { return _type; }
		}
	

		/// <summary>
		/// The class name to identify the typeAlias.
		/// </summary>
		/// <example>Com.Site.Domain.Product</example>
		[XmlAttribute("type")]
		public string TypeName
		{
			get { return _typeName; }
			set 
			{ 
				if ((value == null) || (value.Length < 1))
				{
					throw new ArgumentNullException("The class attribute is mandatory in the typeAlias " + _name);
				}
				_typeName = value; 
			}
		}
		#endregion

		#region Constructor (s) / Destructor
		/// <summary>
		/// Do not use direclty, only for serialization.
		/// </summary>
		public Alias()
		{
        }

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="type">a type.</param>
		public Alias(Type type)
		{
			_type = type;
		}
		#endregion

		#region Methods
		/// <summary>
		/// Initialize the object, 
		/// try to idenfify the .Net type class from the corresponding name.
		/// </summary>
		public void Initialize()
		{
            _type = TypeManager.ResolveType(_typeName);
		}
		#endregion
	}
}
